
"""
Created on Tue Feb 19 14:55:43 2013

@author: aponteeduarddo@gmail.com
Copyright Eduardo Aponte 2013.
"""

from mex_binder import binder_py

eyelink_send_message_ex = binder_py('mx_eyelink_send_message_ex')
eyelink_send_message_ex.mx_c.headers = ['<eyelink.h>','"mex.h"','"utils.h"']
eyelink_send_message_ex.bindo += ['utils.o']
eyelink_send_message_ex.mx_c.code = \
"""
INT16 _main( (UINT32 ) msec, char FARTYPE *msg )
{
    return eyelink_send_message_ex( msec, msg );
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;
    char FARTYPE *msg;
    char addr[ELINKADDRSIZE + 1];
    ELINKADDR eladdr;
    int i, m;

    /* Verify input arguments. */
    if(nrhs != 2 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 1 ) {
        mexErrMsgTxt("Too many output arguments.");
    }
        
    if ( isscalar( prhs[0] ) ){
        /* Returns the scalar, casts to int if necessary. Returns 0 if */
        /* input is complex. */
        if ( returnscalar( prhs[0], &m ) != 1 )
            mexErrMsgTxt("Complex data is not supported.");                
        if ( m < 0 )
            mexErrMsgTxt("mssec should be positive.");
    } else {
        mexErrMsgTxt("msec should be a scalar."); 
    }
    
    if (!mxIsString(prhs[1]))
        mexErrMsgTxt("msg should be a string.");

    if (mxGetNumberOfDimensions(prhs[1] != 2)
        mexErrMsgTxt("msg should be two dimensional.");
        
    msg = mxCalloc(mxGetM(prhs[1]) * mxGetN(prhs[1]) + 1 , sizeof(char));
    
    mxGetString(prhs[1],msg,mxGetM(prhs[1]) * mxGetN(prhs[1]) + 1);
    
    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status = mxGetPr(plhs[0]);
    
    *status = _main( (UINT32 ) m, msg );

    mxFree(msg);

    return;
}
"""
eyelink_send_message_ex.mx_c.mdoc = \
"""function [ ] = mx_eyelink_send_message_ex( )
%MX_EYELINK_SEND_MESSAGE_EX Send a data file message string to connected tracker
% with time offset.
%
% Parameters:
% exectime 	time offset. If the offset is 0, this function is the same as eyelink_send_message()
% msg 	Message to be sent
% Returns:
% OK_RESULT or LINK_TERMINATED_RESULT.
%
%
% OK_RESULT 0;
% LINK_TERMINATED_RESULT -100;
"""
eyelink_send_message_ex.write_c()
eyelink_send_message_ex.write_m()
eyelink_send_message_ex.compile()
eyelink_send_message_ex.write_sh()

